package com.awenhui.demo.core.utils;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

/**
 * @author yuxf
 * @version 1.0
 * @date 2020/8/11 19:41
 */
public class HttpClientUtils {
    public static final CloseableHttpClient httpClient;

    static {
        //设置超时时间
        RequestConfig config = RequestConfig.custom().setConnectTimeout(6000).setSocketTimeout(15000).setConnectionRequestTimeout(6000).build();
        httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
    }

    /**
     * Get请求
     *
     * @param url 请求地址，如： http://139.159.190.0:8080/sail-web/demo/demo01
     * @return
     * @throws IOException
     */
    public static String get(String url) throws IOException {
        HttpGet httpGet = new HttpGet(url);
        return getResponse(httpGet);
    }

    public static String postFormUrlEncodeData(String url, Map<String, String> headers, String body) throws IOException {
        return post(url, headers, body, ContentType.APPLICATION_FORM_URLENCODED);
    }

    public static String postApplicationJsonData(String url, Map<String, String> headers, String body) throws IOException {
        return post(url, headers, body, ContentType.APPLICATION_JSON);
    }

    /**
     * post请求
     *
     * @param url         请求地址
     * @param headers     请求头
     * @param body        请求体
     * @param contentType
     * @return
     */
    public static String post(String url, Map<String, String> headers, String body, ContentType contentType) throws IOException {
        HttpPost httpPost = new HttpPost(url);
        //设置请求体
        StringEntity stringEntity = new StringEntity(body, contentType);
        httpPost.setEntity(stringEntity);
        RequestConfig config = httpPost.getConfig();
        //设置请求头
        if (headers != null && headers.size() > 0) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                httpPost.setHeader(entry.getKey(), entry.getValue());
            }
        }
        return getResponse(httpPost);
    }

    /**
     * 返回响应结果
     *
     * @param httpMethod
     * @return
     * @throws IOException
     */
    static String getResponse(HttpUriRequest httpMethod) throws IOException {
        CloseableHttpResponse response = httpClient.execute(httpMethod);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 200) throw new RuntimeException("HttpClient,error status code :" + statusCode);
        String responseContent = EntityUtils.toString(response.getEntity());
        response.close();
        return responseContent;
    }
}
